<?php
//说明
//  提交方式:   POST
//  提交参数:   request_line        输入的URI
//              appkey              应用的AppKey  
//


//处理过程
$ret = Process();
echo $ret['msg'];

//================函数定义========================
function Process()
{
    $request_line = $_POST['request_line'];

     //拆解开?分隔的URI
    $segs = explode('?', $request_line);
    if (count($segs) != 2)
    {
        return array('code'=>1, 'msg'=>'错误的请求数据');
    }

    $AppKey = $_POST['appkey'];
    $Method = $_POST['method'];
    $SecretKey = $AppKey.'&';

    $uri = $segs[0];

    $pairs = explode('&', $segs[1]);
    foreach ($pairs as $k=>$v)
    {
        $tmp = explode('=', $v, 2);
		if (count($tmp) != 2)
		{
			echo "无效参数: $tmp";
		}
        $params[$tmp[0]] = $tmp[1];
    }
	
    //打印每一条拆解后的内容
    echo "按照您提交的信息，对请求参数进行拆分，结果如下（请仔细核对，如果拆分结果不对，则后面的步骤都是错误的）<br>";
    echo "<table width=\"500\" border=\"0\">";
    foreach($params as $k=>$v)
    {
        echo "<tr>";
        echo "<td align=\"right\"><strong class=\"black\">[$k] : </strong></td>";
        echo "<td align=\"left\"><strong class=\"black\">[$v]</strong></td>";
        echo "</tr>";
    }
    echo "</table>";

    echo "<br>";
    echo "<br>";

    //第一步，得到源串
    echo "第一步，按OpenAPI V3.0的签名生成说明，构造源串。得到的源串为:<br>";
    unset($params['sig']);
    $SourceString = MakeSourceString($Method, $uri, $params);
    echo "&nbsp;&nbsp;&nbsp;&nbsp;<strong class=\"black\">$SourceString</strong>";
    echo "<br>";
    echo "<br>";
    echo "<br>";

    //第二步，给出处理后过的SecretKey
    echo "第二步，按OpenAPI V3.0的签名生成说明，生成SecretKey:<br>";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;<strong class=\"black\">$SecretKey</strong>";
    echo "<br>";
    echo "<br>";
    echo "<br>";

    //第三步，拿SecretKey和SourceString做SHA1-HMAC
    $Base64String = MakeSignature($SourceString, $SecretKey);
    echo "第三步，按OpenAPI V3.0的签名生成说明，生成的签名如下:<br>";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;<strong class=\"red\"> $Base64String </strong>";

    //准备生成正式的请求链接
	$params['sig'] = $Base64String;
	$pairs = array();
	foreach ($params as $k => $v)
	{
		$pairs[] = "$k=".rawurlencode($v);
	}	

	$new_request_line = $uri . "?" . implode ('&', $pairs);
    echo "<br>";
    echo "<br>";
    echo "<br>";

	echo "如果是GET请求, 如下URL可直接贴Firefox地址栏测试";
    echo "<br>";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;<strong class=\"black\">http://119.147.19.43" . $new_request_line . "</strong>";
    echo "<br>";
    echo "<br>";


}

function newline()
{
    echo "</br>";
}

/*
 * 生成源串，源串说明
 * @param string    $Method             请求方法，一般为"Get"或"Post"
 * @param string    $UriPath            请求的URI路径                
 * @param string    $Param              通过URI附加的参数，如test.cgi?name=xxx&sex=man&age=18
 *
 * @return string                       返回合成的源串
 */
function MakeSourceString($Method,$URI,$Param)
{
    //先进行排序
    ksort ( $Param );

    //再把请求方法和URI路径用&号连接起来
    $MethodAndUri = strtoupper($Method) . '&' . rawurlencode ( $URI ) . '&';

    //合并字符串
    $TempString = ""; 
    foreach ( $Param as $key => $val ) { 
        $TempString .= "$key=$val&";
    }   

    //去掉最后的&号
    $NameAndValue = substr ( $TempString, 0, strlen ( $TempString ) - 1 );

    //合并
    return $MethodAndUri . rawurlencode ( $NameAndValue );
}

/*
 * 生成签名
 * @param string $SourceString      已经构造完毕的源串             
 * @param string $SecretKey         合成的密钥
 */
function MakeSignature($SourceString, $SecretKey)
{
    $HashRet = hash_hmac("sha1", $SourceString, strtr( $SecretKey, '-_', '+/' ), true);
    $Base64Ret = base64_encode( $HashRet );

    return $Base64Ret;
}

?>
